<?php
/**
 * ProductTypesController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-13 23:58:40
 * @modified    2023-02-13 23:58:40
 *
 */

namespace App\Http\Controllers;

use App\Repositories\Product\TypeRepo;
use Illuminate\Http\Request;

class ProductTypesController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/product_types",
     *     tags={"ProductType"},
     *     summary="获取商品类型列表",
     *     description="获取商品类型列表",
     *     operationId="product_type_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="类型名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="warranty_start_type", in="query", description="质保开始类型", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="category_id", in="query", description="分类ID", required=false, @OA\Schema(default=0, type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $types = TypeRepo::getList();
        return list_message("获取成功", true, $types);
    }

    /**
     * @OA\Get(
     *     path="/product_types/{id}",
     *     tags={"ProductType"},
     *     summary="获取商品类型详情",
     *     description="获取商品类型详情",
     *     operationId="product_type_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="商品类型ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $bom = TypeRepo::fetch($id);
            return message("获取成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/product_types",
     *     tags={"ProductType"},
     *     summary="添加类型",
     *     description="添加类型",
     *     operationId="product_types_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"category_id", "name", "image", "intro", "warranty_start_type", "warranty_cycle_count", "warranty_start_unit"},
     *              @OA\Property(property="category_id", type="integer", example=0, description="分类ID"),
     *              @OA\Property(property="name", type="string", example="", description="类型名称"),
     *              @OA\Property(property="image", type="string", example="", description="图片"),
     *              @OA\Property(property="intro", type="string", example="", description="简介"),
     *              @OA\Property(property="warranty_start_type", type="string", example="", description="质保开始类型"),
     *              @OA\Property(property="warranty_start_unit", type="string", example="", description="质保周期单位"),
     *              @OA\Property(property="warranty_cycle_count", type="integer", example=0, description="质保周期时长")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $bom = TypeRepo::store(0);
            return message("创建成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put(
     *     path="/product_types/{id}",
     *     tags={"ProductType"},
     *     summary="更新类型",
     *     description="更新类型",
     *     operationId="product_types_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="商品类型ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"category_id", "name", "image", "intro", "warranty_start_type", "warranty_cycle_count", "warranty_start_unit"},
     *              @OA\Property(property="category_id", type="integer", example=0, description="分类ID"),
     *              @OA\Property(property="name", type="string", example="", description="类型名称"),
     *              @OA\Property(property="image", type="string", example="", description="图片"),
     *              @OA\Property(property="intro", type="string", example="", description="简介"),
     *              @OA\Property(property="warranty_start_type", type="string", example="", description="质保开始类型"),
     *              @OA\Property(property="warranty_start_unit", type="string", example="", description="质保周期单位"),
     *              @OA\Property(property="warranty_cycle_count", type="integer", example=0, description="质保周期时长"),
     *              @OA\Property(
     *                  property="boms",
     *                  type="array",
     *                  @OA\Items(
     *                      @OA\Property(property="product_bom_id", type="integer", example=0, description="商品配件ID"),
     *                      @OA\Property(property="count", type="integer", example=0, description="数量"),
     *                  )
     *              )
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $bom = TypeRepo::store($id);
            return message("更新成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/product_types/{id}",
     *     tags={"ProductType"},
     *     summary="删除类型",
     *     description="删除类型",
     *     operationId="product_type_destroy",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="类型ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            TypeRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/product_types/params",
     *     tags={"ProductType"},
     *     summary="获取商品类型参数列表",
     *     description="获取商品类型参数列表",
     *     operationId="product_type_params",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function params(Request $request)
    {
        $params = TypeRepo::params();
        return message("获取成功", true, $params);
    }
}